<?php
namespace  common\components\services\orders;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\ArtTeaching;
use zhtfrontend\models\datas\ClassManage;
use zhtfrontend\models\datas\ClassStudent;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\GoodsAttr;
use zhtfrontend\models\datas\GoodsGroup;
use zhtfrontend\models\datas\GoodsPrice;
use zhtfrontend\models\datas\Orders;
use zhtfrontend\models\datas\OrdersItems;
use zhtfrontend\models\datas\OrdersItemsRelation;
use zhtfrontend\models\datas\OrdersLog;
use zhtfrontend\models\datas\OrdersType;
use common\models\datas\Student;
use yii\base\Component;
use yii\base\ErrorException;
use yii\db\Exception;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/27
 * Time: 16:41
 */
class ZhtOper extends Component
{
    /**
     * 订单操作记录
     * @param $option
     */
    public static function addOrderLog($option){
        $log = new OrdersLog();
        $log->orders_id = $option['orders_id'];
        $log->operator_id = \Yii::$app->user->id;
        $log->operator_name = \Yii::$app->user->identity->name;
        $log->status = $option['status'];
        $log->result = $option['result'];
        $log->log_text = $option['log_text'];
        $log->alttime = time();
        $log->save(false);
    }

    /**
     * 单个商品插入orderitem
     * @param object $model
     * @param object $goods
     * @param object $customer
     */
    public static function perfectOrder($model,$goods,$customer,$price_id,$art_id=0){
            $attrs = GoodsAttr::find()->where(['goods_id' => $goods->id])->asArray()->all();
            $student = Student::find()->where(['customer_id' => $customer->id])->one();
            //插入课程类型
            $orderType = new OrdersType();
            $orderType->orders_id = $model->id;
            $orderType->type_id = $goods->type_id;
            $orderType->amount = $model->final_amount;
            $orderType->quantity = 1;
            $orderType->save(false);


            //插入订单课程
            $goodsPrice = GoodsPrice::findOne($price_id);
            if($goodsPrice->activity_start_time <= time() && $goodsPrice->activity_end_time >= time() && $goodsPrice->effective_number > 0 ){
                $goodsPrice->effective_number -=1;
                $goodsPrice->save(false);
            }
            if($goodsPrice->is_activity ==1 && (time() > $goodsPrice->activity_start_time && time() < $goodsPrice->activity_end_time)){
                $price = $goodsPrice->lesson_price;
            }else{
                $price = $goodsPrice->market_price;
            }
            $orderItem = new OrdersItems();
            $orderItem->orders_id = $model->id;
            $orderItem->orders_type_id = $orderType->id;
            $orderItem->goods_id = $goods->id;
            $orderItem->mechanic_id = $goods->mechanic_id;
            $orderItem->sn = $goods->sn;
            $orderItem->name = $goods->name;
            $orderItem->cost = $goods->cost;
            $orderItem->price = $price;
            $orderItem->market_price = $price;
//            if($goods->discount_type == 1){
//                $price = $goods->price - $goods->price*($goods->reduce/100);
//                $orderItem->price = $price;
//                $orderItem->market_price = $price;
//            }
//            if($goods->discount_type == 2){
//                $price = $goods->price - $goods->reduce;
//                $orderItem->price = $price;
//                $orderItem->market_price = $price;
//            }
//            $final_amount = $model->final_amount;
//            $price_proportion = $price/$final_amount;
//            $cash_money = $model->cash_money*$price_proportion;
//            $scholarship_money = $model->scholarship_money*$price_proportion;

            $orderItem->amount = $price * 1;
            $orderItem->give_points = $goods->score;
            $orderItem->nums = 1;
            $orderItem->goods_attr = serialize($attrs);
            $orderItem->student_id = $student->id;
            $orderItem->cash_money = 0;
            $orderItem->actual_cash_money = 0;
            if ($orderItem->save(false)) {
                $orderItemPrice = new OrdersItemsRelation();
                $orderItemPrice->orders_id = $model->id;
                $orderItemPrice->orders_items_id = $orderItem->id;
                $orderItemPrice->lesson = $goodsPrice->lesson;
                $orderItemPrice->type = 'consume';
                $orderItemPrice->customer_id = $model->customer_id;
                $orderItemPrice->student_id = $student->id;
                $orderItemPrice->goods_id = $goods->id;
                $orderItemPrice->teach_type = 1;
                $orderItemPrice->price_name = $goodsPrice->price_name;
                $orderItemPrice->lesson_price = $price;
                $orderItemPrice->distribution_way = $goodsPrice->distribution_way;
                $orderItemPrice->distribution_proportion = $goodsPrice->distribution_proportion;
                $orderItemPrice->subsidy_type = $goodsPrice->subsidy_type;
                $orderItemPrice->subsidy_money = $goodsPrice->subsidy_money;
                $orderItemPrice->is_scholarship = $goodsPrice->is_scholarship;
                $orderItemPrice->scholarship = $goodsPrice->scholarship;
                $orderItemPrice->art_lesson = $goodsPrice->art_lesson;
                $orderItemPrice->art_lesson_price = $goodsPrice->art_lesson_price;
                $orderItemPrice->is_art_scholarship = $goodsPrice->is_art_scholarship;
                $orderItemPrice->art_scholarship = $goodsPrice->art_scholarship;
                $orderItemPrice->goods_price_id = $price_id;
                $orderItemPrice->is_frequency = $goodsPrice->is_frequency;
                $orderItemPrice->deadline_type = $goodsPrice->deadline_type;
                $orderItemPrice->deadline = $goodsPrice->deadline;
                $orderItemPrice->save(false);

//                if($art_id > 0){
//                    $artModel = ArtTeaching::findOne($art_id);
//                    $AorderItem = new OrdersItems();
//                    $AorderItem->orders_id = $model->id;
//                    $AorderItem->orders_type_id = $orderType->id;
//                    $AorderItem->goods_id = $artModel->id;
//                    $AorderItem->name = $artModel->title;
//                    $AorderItem->cost = $goodsPrice->art_lesson_price;
//                    $AorderItem->price = $goodsPrice->art_lesson_price;
//                    $AorderItem->market_price = $artModel->price;
//                    $AorderItem->amount =  $AorderItem->price;
//                    $AorderItem->give_points = 0;
//                    $AorderItem->goods_attr = '';
//                    $AorderItem->student_id = $student->id;
//                    $AorderItem->cash_money = 0;
//                    $AorderItem->actual_cash_money = 0;
//                    $AorderItem->teach_type = 2;
//                    if($AorderItem->save(false)){
//                        $Aclass = ClassManage::find()->where(['is_old' => 0, 'associate_id' => $artModel->id,'type'=>2])->one();
//                        $AClassStudentModel = new ClassStudent();
//                        $AClassStudentModel->class_manage_id = $Aclass->class_id;
//                        $AClassStudentModel->order_sales_id = $AorderItem->id;
//                        $AClassStudentModel->student_id = $student->id;
//                        $AClassStudentModel->save(false);

//                    $artModel->max_count = 1;
//                    $artModel->save(false);
//                    }


//                }

//                    if ($goods->type_id == 1) {
//                        $class = ClassManage::find()->where(['is_old' => 0, 'associate_id' => $goods->id,'type'=>1])->one();
//                        $ClassStudentModel = new ClassStudent();
//                        $ClassStudentModel->class_manage_id = $class->class_id;
//                        $ClassStudentModel->order_sales_id = $orderItem->id;
//                        $ClassStudentModel->student_id = $student->id;
//                        $ClassStudentModel->save(false);
//                    }
            }
    }

    /**
     * 多个商品插入ordersitem
     * @param object $model
     * @param array $gids 商品id
     * @param object $customer
     */
    public static function perfectOrders($model,$gids,$customer,$orderType = ''){
        $student = Student::find()->where(['customer_id' => $customer->id])->one();
        if($gids){
            $discount_amount = $model->discount_amount/$model->total_amount; //计算优惠多少钱
            foreach($gids as $id){

                $goods = Goods::findOne($id);
                if($goods->type_id == 5){
                    $goodsGroupList = GoodsGroup::find()->where(['goods_id'=>$goods->id])->all();
                    foreach($goodsGroupList as $group){
                        $groupRow = Goods::findOne($group->group_id);
                        $price = 0;
                        $cost_price = 0;
                        $market_price = 0;
                        if($group->discount_type == 1){
                            $price = $groupRow->price - ($groupRow->price*$group->price_reduce/100);
                            $cost_price = $groupRow->cost - ($groupRow->cost*$group->price_reduce/100);
                            $market_price = $groupRow->market_price - ($groupRow->market_price*$group->price_reduce/100);
                        }else{
                            $price = $groupRow->price - $group->price_reduce;
                            $cost_price = $groupRow->cost - $group->cost_reduce;
                            $market_price = $groupRow->market_price - $group->market_price_reduce;
                        }
                        $orderType = OrdersType::find()->where(['type_id'=>$groupRow->type_id,'orders_id'=>$model->id])->one();
                        if($orderType == null) {
                            //插入订单类型
                            $orderType = new OrdersType();
                            $orderType->orders_id = $model->id;
                            $orderType->type_id = $groupRow->type_id;
                            $orderType->amount = $price;
                            $orderType->quantity = 1;
                        }else{
                            $orderType->amount += $price;
                            $orderType->quantity += 1;
                        }
                        $orderType->save(false);
                        $groupRowattrs = GoodsAttr::find()->where(['goods_id' => $group->group_id])->asArray()->all();
                        $orderItem = new OrdersItems();
                        $orderItem->orders_id = $model->id;
                        $orderItem->goods_id = $groupRow->id;
                        $orderItem->orders_type_id = $orderType->id;
                        $orderItem->mechanic_id = $groupRow->mechanic_id;
                        $orderItem->sn = $groupRow->sn;
                        $orderItem->name = $groupRow->name;
                        $orderItem->cost = $cost_price;
                        $orderItem->price = $price;
                        $orderItem->market_price = $market_price;
                        $orderItem->amount = $price * 1;
                        $orderItem->give_points = $groupRow->score;
                        $orderItem->nums = 1;
                        $orderItem->goods_attr = serialize($groupRowattrs);
                        $orderItem->student_id = $student->id;
                        $orderItem->group_id = $goods->id;
                        $orderItem->cash_money = $price; //使用余额金额
                        $orderItem->actual_cash_money = $price; //最终使用余额金额
                        if ($orderItem->save(false)){
                            if ($goods->type_id == 1) {
                                $class = ClassManage::find()->where(['is_old' => 0, 'associate_id' => $goods->id])->one();
                                $ClassStudentModel = new ClassStudent();
                                $ClassStudentModel->class_manage_id = $class->class_id;
                                $ClassStudentModel->order_sales_id = $orderItem->id;
                                $ClassStudentModel->student_id = $student->id;
                                $ClassStudentModel->save(false);
                            }
                        }
                        $groupRow->store -= 1;
                        $groupRow->save(false);
                    }
                }else {
                    $attrs = GoodsAttr::find()->where(['goods_id' => $id])->asArray()->all();
                    $orderType = OrdersType::find()->where(['orders_id'=>$model->id,'type_id'=>$goods->type_id])->one();
                    if($orderType != null){
                        $orderType->quantity += 1;
                        $orderType->amount += ($goods->price)-($goods->price*$discount_amount);
                    }else{
                        $orderType = new OrdersType();
                        $orderType->orders_id = $model->id;
                        $orderType->type_id = $goods->type_id;
                        $orderType->amount = ($goods->price)-($goods->price*$discount_amount);
                        $orderType->quantity = 1;
                    }
                    $orderType->save(false);
                    //插入订单课程
                    $orderItem = new OrdersItems();
                    $orderItem->orders_id = $model->id;
                    $orderItem->orders_type_id = $orderType->id;
                    $orderItem->goods_id = $goods->id;
                    $orderItem->mechanic_id = $goods->mechanic_id;
                    $orderItem->sn = $goods->sn;
                    $orderItem->name = $goods->name;
                    $orderItem->cost = $goods->cost;
                    $orderItem->price = $goods->price;
                    $orderItem->market_price = $goods->market_price;
                    $orderItem->amount = $goods->price * 1;
                    $orderItem->give_points = $goods->score;
                    $orderItem->nums = 1;
                    $orderItem->goods_attr = serialize($attrs);
                    $orderItem->student_id = $student->id;

                    $price = ($goods->price) - ($goods->price * $discount_amount); //优惠后的单价
                    $total = $model->final_amount; //实付金额
                    $proportion = $price / $total; //计算比例

                    $orderItem->scholarship_money = ceil($proportion * $model->scholarship_money); //使用奖学金金额
                    $orderItem->actual_scholarship_money = ceil($proportion * $model->scholarship_money); //最终使用奖学金金额
                    $orderItem->cash_money = ceil($proportion * $model->cash_money); //使用余额金额
                    $orderItem->actual_cash_money = ceil($proportion * $model->cash_money); //最终使用余额金额
                    if ($orderItem->save(false)) {
                        if ($goods->type_id == 1) {
                            $class = ClassManage::find()->where(['is_old' => 0, 'associate_id' => $goods->id])->one();
                            $ClassStudentModel = new ClassStudent();
                            $ClassStudentModel->class_manage_id = $class->class_id;
                            $ClassStudentModel->order_sales_id = $orderItem->id;
                            $ClassStudentModel->student_id = $student->id;
                            $ClassStudentModel->save(false);
                        }

                    }
                }
                $goods->store -= 1;
                $goods->save(false);
            }
        }

    }
    /**
     * 多个实物商品插入ordersitem
     * @param object $model
     * @param array $gids 商品id
     * @param object $customer
     */
    public static function addorderitem($order,$carts){
        $student = Student::find()->where(['customer_id' => $order->customer_id])->one();
        if($carts){
            foreach ($carts[0] as $goods_id){
                $num = $carts[1][$goods_id]; //购买单个商品数量
                $goods = Goods::findOne($goods_id);
                $orderType = OrdersType::find()->where(['orders_id'=>$order->id,'type_id'=>$goods->type_id])->one();
                if($orderType != null){
                    $orderType->quantity += $num;
                    $orderType->amount += $goods->price*$num;
                }else{
                    $orderType = new OrdersType();
                    $orderType->orders_id = $order->id;
                    $orderType->type_id = $goods->type_id;
                    $orderType->amount = $goods->price*$num;
                    $orderType->quantity = $num;
                }
                $orderType->save(false);

                //插入订单课程
                $orderItem = new OrdersItems();
                $orderItem->orders_id = $order->id;
                $orderItem->orders_type_id = $orderType->id;
                $orderItem->goods_id = $goods->id;
                $orderItem->mechanic_id = $goods->mechanic_id;
                $orderItem->sn = $goods->sn;
                $orderItem->name = $goods->name;
                $orderItem->cost = $goods->cost;
                $orderItem->price = $goods->price;
                $orderItem->market_price = $goods->market_price;
                $orderItem->amount = $goods->price * $num;
                $orderItem->give_points = $goods->score;
                $orderItem->nums = $num;
                $orderItem->goods_attr = '';
                $orderItem->student_id = $student->id;
                $orderItem->actual_cash_money = $goods->price * $num;
                $orderItem->cash_money = $goods->price * $num;
                $orderItem->save(false);

                $goods->store -= $num;
                $goods->save(false);
            }
        }
    }

    public static function addContinueItem($model,$goods,$customer,$lesson_price,$art_lesson_price){
        $attrs = GoodsAttr::find()->where(['goods_id' => $goods->id])->asArray()->all();
        $student = Student::find()->where(['customer_id' => $customer->id])->one();
        //插入课程类型
        $orderType = new OrdersType();
        $orderType->orders_id = $model->id;
        $orderType->type_id = $goods->type_id;
        $orderType->amount = $model->final_amount;
        $orderType->quantity = 1;
        $orderType->save(false);
        //插入订单课程
        $lesson_price_total = $lesson_price*$model->skill_class_num;
        $orderItem = new OrdersItems();
        $orderItem->orders_id = $model->id;
        $orderItem->orders_type_id = $orderType->id;
        $orderItem->goods_id = $goods->id;
        $orderItem->mechanic_id = $goods->mechanic_id;
        $orderItem->sn = $goods->sn;
        $orderItem->name = $goods->name;
        $orderItem->cost = $lesson_price_total;
        $orderItem->price = $lesson_price_total;
        $orderItem->market_price = $lesson_price_total;
        $orderItem->amount = $lesson_price_total * 1;
        $orderItem->give_points = $goods->score;
        $orderItem->nums = 1;
        $orderItem->goods_attr = serialize($attrs);
        $orderItem->student_id = $student->id;
        $orderItem->cash_money = 0;
        $orderItem->actual_cash_money = 0;
        if($orderItem->save(false)){
            $modelItemsRelation = new OrdersItemsRelation();
            $modelItemsRelation->orders_id=$model->id;
            $modelItemsRelation->orders_items_id=$orderItem->id;
            $modelItemsRelation->lesson=$model->skill_class_num;
            $modelItemsRelation->type=$model->order_type;
            $modelItemsRelation->customer_id=$model->customer_id;
            $modelItemsRelation->student_id=$student->id;
            $modelItemsRelation->goods_id=$goods->id;
            $modelItemsRelation->teach_type=1;
            $modelItemsRelation->save(false);
        }
        $group = GoodsGroup::find()->where(['goods_id'=>$goods->id])->one();
        if($group != null) {
            $art_lesson_price_total = $art_lesson_price*$model->art_class_num;
            $artModel = ArtTeaching::findOne($group->group_id);
            $AorderItem = new OrdersItems();
            $AorderItem->orders_id = $model->id;
            $AorderItem->orders_type_id = $orderType->id;
            $AorderItem->goods_id = $artModel->id;
            $AorderItem->name = $artModel->title;
            $AorderItem->cost = $art_lesson_price_total;
            $AorderItem->price = $art_lesson_price_total;
            $AorderItem->market_price = $art_lesson_price_total;
            $AorderItem->amount = $art_lesson_price_total;
            $AorderItem->give_points = 0;
            $AorderItem->goods_attr = '';
            $AorderItem->student_id = $student->id;
            $AorderItem->cash_money = 0;
            $AorderItem->actual_cash_money = 0;
            $AorderItem->teach_type = 2;
            if($AorderItem->save(false)){
                $modelItemsRelation = new OrdersItemsRelation();
                $modelItemsRelation->orders_id=$model->id;
                $modelItemsRelation->orders_items_id=$AorderItem->id;
                $modelItemsRelation->lesson=$model->art_class_num;
                $modelItemsRelation->type=$model->order_type;
                $modelItemsRelation->customer_id=$model->customer_id;
                $modelItemsRelation->student_id=$student->id;
                $modelItemsRelation->goods_id=$artModel->id;
                $modelItemsRelation->teach_type=2;
                $modelItemsRelation->save(false);
            }
        }

    }


}